<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//后台商品控制器
class Goods extends Admin_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('admin/goodstype_model');
        $this->load->model('admin/attribute_model');
        $this->load->model('admin/category_model');
        $this->load->model('admin/brand_model');
        $this->load->model('admin/goods_model');
        $this->load->library('form_validation');
    }
    //产品列表
    public function index()
    {
        $data['goods'] = $this->goods_model->list_goods();
        $this->load->view('product-list.html',$data);
    }
    //添加产品
    public function add()
    {
        #获取所有的商品类型信息
        $data['goodstypes'] = $this->goodstype_model->get_all_types();
        #获取分类信息
        $data['cates'] = $this->category_model->list_cate();
        #获取品牌信息
        $data['brands'] = $this->brand_model->list_brand();
        $this->load->view('goods_add.html',$data);
    }

    #添加商品
    public function insert(){

        #设置验证规则
        $this->form_validation->set_rules('goods_name','商品名称','trim|required');
        $this->form_validation->set_rules('cat_id','商品分类','trim|required|is_natural_no_zero');
        $this->form_validation->set_rules('shop_price','本店售价','trim|required|numeric');


        if ($this->form_validation->run() == false) {
            # code...
            $data['message'] = validation_errors();
            $data['wait'] = 5;
            $data['url'] = base_url().'admin/goods/index';
            $this->load->view('message.html', $data);
        }else{
            #获取提交的数据
            $data['goods_name'] = $this->input->post('goods_name');
            //$data['goods_sn'] = 'BPH' . date('YmdHis') . mt_rand(10000,99999);
            $data['cat_id'] = $this->input->post('cat_id');
            //$data['brand_id'] = $this->input->post('brand_id');
            $data['market_price'] = $this->input->post('market_price');
            $data['shop_price'] = $this->input->post('shop_price');
            $is_promote = $this->input->post('is_promote');
            if($is_promote==1){
                $data['promote_price'] = $this->input->post('promote_price');
                $data['promote_start_time'] = strtotime($this->input->post('promote_start_time'));
                $data['promote_end_time'] = strtotime($this->input->post('promote_end_time'));
            }

            $data['goods_number'] = $this->input->post('goods_number');
            $data['type_id'] = $this->input->post('type_id');
            $data['goods_brief'] = $this->input->post('goods_brief');
            $data['is_best'] = $this->input->post('is_best');
            $data['is_new'] = $this->input->post('is_new');
            $data['is_hot'] = $this->input->post('is_hot');
            $data['is_on_sale'] = $this->input->post('is_on_sale');
            $data['goods_desc'] = $this->input->post('editorValue');


            $name = array();

            //初始化
            $config['upload_path'] = './public/uploads/goods_img';
            if(!file_exists('./public/uploads/goods_img')){
                mkdir('public/uploads',0777,true);//存储路径
            }
            $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
            $config['encrypt_name'] = TRUE;
            $config['remove_spaces'] = TRUE;
            $config['max_size']  = '0';
            $config['max_width']  = '0';
            $config['max_height']  = '0';
            $this->load->library('upload', $config);



            //中缩略图片
            $config2 = array();
            $config2['image_library'] = 'gd2';
            $config2['source_image'] = '';
            $config2['create_thumb'] = TRUE;
            $config2['maintain_ratio'] = TRUE; //保持图片比例
            $config2['new_image'] = './public/uploads/goods_thumb';
            if(!file_exists('./public/uploads/goods_thumb')){
                mkdir('public/uploads',0777,true);//存储路径
            }
            $config2['width'] = 218;
            $config2['height'] = 218;


            //中图片
            $configThumb = array();
            $configThumb['image_library'] = 'gd2';
            $configThumb['source_image'] = '';
            $configThumb['create_thumb'] = TRUE;
            $configThumb['maintain_ratio'] = TRUE; //保持图片比例
            $configThumb['new_image'] = './public/uploads/goods_mid_img';
            if(!file_exists('./public/uploads/goods_mid_img')){
                mkdir('public/uploads',0777,true);//存储路径
            }
            $configThumb['width'] = 350;
            $configThumb['height'] = 350;


            //小图片
            $configLarge = array();
            $configLarge['image_library'] = 'gd2';
            $configLarge['source_image'] = '';
            $configLarge['create_thumb'] = TRUE;
            $configLarge['maintain_ratio'] = TRUE; //保持图片比例
            $configLarge['new_image'] = './public/uploads/goods_small_img';
            $configLarge['width'] = 60;
            $configLarge['height'] = 60;
            $this->load->library('image_lib');
            for($i = 1; $i < 5; $i++) {
                $upload = $this->upload->do_upload('image' . $i);
                if ($upload === FALSE) continue;
                $dataimg = $this->upload->data();//返回上传文件的所有相关信息的数组
                array_push($name,$dataimg['file_name']);
                $uploadedFiles[$i] = $dataimg;
                if ($dataimg['is_image'] == 1) {

                    //初始化缩略图
                    $config2['source_image'] = $dataimg['full_path']; //文件路径带文件名
                    $this->image_lib->initialize($config2);
                    $this->image_lib->resize();



                    //初始化中图
                    $configThumb['source_image'] = $dataimg['full_path']; //文件路径带文件名
                    $this->image_lib->initialize($configThumb);
                    $this->image_lib->resize();
                    //初始化小图
                    $configLarge['source_image'] = $dataimg['full_path']; //文件路径带文件名
                    $this->image_lib->initialize($configLarge);
                    $this->image_lib->resize();
                }
            }




            $data['goods_img'] = implode(',',$name);

            if ($goods_id = $this->goods_model->add_goods($data)) {
                # 添加商品成功,获取属性并插入到商品属性关联表中
                $attr_ids = $this->input->post('attr_id_list');
                $attr_values = $this->input->post('attr_value_list');
                foreach ($attr_values as $k => $v) {
                    if (!empty($v)) {
                        $data2['goods_id'] = $goods_id;
                        $data2['attr_id'] = $attr_ids[$k];
                        if(is_array($v)){
                            $v = implode(',',$v);
                        }
                        $data2['attr_value'] = $v;

                        #完成插入
                        $this->db->insert('goods_attr',$data2);
                    }
                }

                $data['message'] = '添加商品成功';
                $data['url'] = base_url().'admin/goods/index';
                $data['wait'] = 5;
                $this->load->view('message.html',$data);


            } else {
                # 失败
                $data['message'] = '添加商品失败';
                $data['url'] = base_url().'admin/goods/add';
                $data['wait'] = 5;
                $this->load->view('message.html',$data);
            }
        }









    }




    public function create_attrs_html(){
        #获取类型id
        $type_id = $this->input->get('type_id');
        // echo $type_id;
        $attrs = $this->attribute_model->get_attrs($type_id);
        #根据获取到的属性值构造html字符串
        $html = '';
        foreach ($attrs as $v) {
            $html .= "<div class=\"row cl\">";
            $html .= "<label class=\"form-label col-xs-4 col-sm-2\">".$v['attr_name']."</label>";
            $html .="<div class=\"formControls col-xs-8 col-sm-9 skin-minimal\">";
            $html .= "<input type='hidden' name='attr_id_list[]' value='".$v['attr_id']."'>";
            switch ($v['attr_input_type']) {
                case 0:
                    # 文本框
                    $html .= "<input name='attr_value_list[]' type='text'  class='input-text'>";
                    break;
                /*case 1:
                    # 复选
                    $arr = explode(PHP_EOL, $v['attr_value']);
                    $value = implode(',',$arr);
                    $html .= "<input name='attr_value_list[]' value='$value' type='text'  class='input-text'>";
                    $html .= "<span style='color: red'>多个属性值请务必使用英文的逗号,分割</span>";
                    break;
                case 2:
                    # 文本域
                    break;*/

                default:
                    # code...
                    break;
            }

            $html .="</div>";
            $html .="</div>";
        }

        echo $html;

    }





}
